<template>
   <!-- 添加或修改外链管理对话框 -->
       <el-dialog title="等级配置" :visible.sync="listShow" width="90%" append-to-body>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="用户" prop="userId">
          <el-select v-model="queryParams.userId" placeholder="请选择" clearable filterable remote reserve-keyword    :remote-method="remoteMethod"
                     :loading="loading" style="width: 100%">
            <el-option
              v-for="dict in userList"
              :key="dict.id"
              :label="dict.nickname"
              :value="dict.id"
            >
              <span style="float: left">{{ dict.nickname }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ dict.phone }}</span>
            </el-option>

          </el-select>

          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

      </el-col>


      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['games:gamesConfig:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['games:gamesConfig:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['games:gamesConfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['games:gamesConfig:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="gamesConfigList" @selection-change="handleSelectionChange" :span-method="spanMethod">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="游戏" align="center" prop="gamesVo.name" />
      <el-table-column label="所属用户" align="center" prop="gamesVo.name">
        <template slot-scope="scope">
            <span>{{parseInt(scope.row.userId)===0?'系统':scope.row.userVo.nickname}}</span>
        </template>
      </el-table-column>
      <el-table-column label="关卡" align="center" prop="level" />
      <el-table-column label="宽度" align="center" prop="width" />
      <el-table-column label="高度" align="center" prop="height" />
      <el-table-column label="列数" align="center" prop="levelCols" />
      <el-table-column label="行数" align="center" prop="levelRows" />
      <el-table-column label="几个对象消除" align="center" prop="groupCount" />
      <el-table-column label="图标" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="图标数量" align="center" prop="imgNum" />
      <el-table-column label="层数" align="center" prop="layerCount" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['games:gamesConfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['games:gamesConfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改游戏配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="游戏" prop="gameId">
              <el-input v-model="form.gamesVo.name" placeholder="请输入游戏" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户" prop="userId">
              <el-select v-model="form.userId" placeholder="请选择" clearable filterable remote reserve-keyword    :remote-method="remoteMethod"
                         :loading="loading" style="width: 100%">
                <el-option
                  v-for="dict in userList"
                  :key="dict.id"
                  :label="dict.nickname"
                  :value="dict.id"
                >
                  <span style="float: left">{{ dict.nickname }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ dict.phone }}</span>
                </el-option>

              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="等级" prop="level">
              <el-input v-model="form.level" placeholder="请输入等级" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宽度" prop="width">
              <el-input v-model="form.width" placeholder="请输入宽度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="高度" prop="height">
              <el-input v-model="form.height" placeholder="请输入高度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="列数" prop="levelCols">
              <el-input v-model="form.levelCols" placeholder="请输入列数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行数" prop="levelRows">
              <el-input v-model="form.levelRows" placeholder="请输入行数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当前级别有几组" prop="groupCount">
              <el-input v-model="form.groupCount" placeholder="当前级别有几组" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="几个对象消除" prop="oneGroupCount">
              <el-input v-model="form.oneGroupCount" placeholder="请输入几个对象消除" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="层数" prop="layerCount">
              <el-input v-model="form.layerCount" placeholder="请输入层数" />
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="当前等级图标数量" prop="imgNum">
              <el-input v-model="form.imgNum" placeholder="请输入数量" />
            </el-form-item>

          </el-col>
        </el-row>

        <el-form-item label="图标" prop="img">
          <image-upload v-model="form.img" :limit="form.imgNum?parseInt(form.imgNum):5"/>
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    </el-dialog>
</template>

<script>
import { listGamesConfig, getGamesConfig, delGamesConfig, addGamesConfig, updateGamesConfig } from "@/api/games/gamesConfig";
import { getUser, listUser } from '@/api/user/user'

export default {
  name: "GamesConfig",
  data() {
    return {
      /**
       * 等级配置
       */
      listShow:false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 游戏配置表格数据
      gamesConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: undefined,
        gameId: undefined,
        width: undefined,
        height: undefined,
        levelCols: undefined,
        levelRows: undefined,
        groupCount: undefined,
        img: undefined,
        userId: undefined,
        level: undefined,
        layerCount: undefined,
      },
      // 表单参数
      form: {
        gamesVo:{
          name:""
        }
      },
      // 表单校验
      rules: {
        gameId: [
          { required: true, message: "游戏Id不能为空", trigger: "blur" }
        ],
        width: [
          { required: true, message: "宽度不能为空", trigger: "blur" }
        ],
        height: [
          { required: true, message: "高度不能为空", trigger: "blur" }
        ],
        levelCols: [
          { required: true, message: "列数不能为空", trigger: "blur" }
        ],
        levelRows: [
          { required: true, message: "行数不能为空", trigger: "blur" }
        ],
        groupCount: [
          { required: true, message: "几个对象消除不能为空", trigger: "blur" }
        ],
        img: [
          { required: true, message: "图标不能为空", trigger: "blur" }
        ],
        level: [
          { required: true, message: "等级不能为空", trigger: "blur" }
        ],
        layerCount: [
          { required: true, message: "层数不能为空", trigger: "blur" }
        ],
        imgNum: [
          { required: true, message: "图标数量不能为空", trigger: "blur" }
        ],
      },
      gamesVo:{},
      userList:[],
    };
  },
  methods: {
    /** 查询游戏配置列表 */
    getList(gamesVo) {
      this.gamesVo = gamesVo;
      this.loading = true;

      this.queryParams.gameId = gamesVo.id;
      listGamesConfig(this.queryParams).then(response => {
        this.gamesConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.listShow = true;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        tenantId: undefined,
        gameId: undefined,
        width: undefined,
        height: undefined,
        levelCols: undefined,
        levelRows: undefined,
        groupCount: undefined,
        oneGroupCount: undefined,
        img: undefined,
        imgNum: undefined,
        userId: undefined,
        level: undefined,
        layerCount: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        gamesVo:{
          name:""
        }
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;

      this.queryParams.gameId = this.gamesVo.id;
      listGamesConfig(this.queryParams).then(response => {
        this.gamesConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.listShow = true;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.gamesVo = this.gamesVo;
      this.form.gameId = this.gamesVo.id;
      this.open = true;
      this.title = "添加游戏等级";


    },
    /** 修改按钮操作 */
   async handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids

      getGamesConfig(id).then(response => {
        this.loading = false;
        this.form = response.data;
        if(response.data.userId && response.data.userId !=="0"){
          getUser(response.data.userId).then(res => {
            this.userList  =[]
            this.userList.push(res.data)
          })
        }

       let arr =  this.form.img.split(",");
        if(arr.length>this.form.imgNum ){
          arr = arr.splice(0,this.form.imgNum);
          this.form.img = arr.join(",");
        }

        this.open = true;
        this.title = "修改游戏配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateGamesConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.handleQuery();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addGamesConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.handleQuery();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除游戏配置编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delGamesConfig(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('games/gamesConfig/export', {
        ...this.queryParams
      }, `gamesConfig_${new Date().getTime()}.xlsx`)
    },
    remoteMethod(query){

      listUser({
        pageNum:1,
        pageSize:1000,
        params:{
          searchValue:query
    }
      }).then(res=>{
      this.userList= res.rows;


      })
    },
    spanMethod({ row, column, rowIndex, columnIndex }){
    if(columnIndex === 1){
      if(rowIndex===0){
        return {
          rowspan: this.gamesConfigList.length,
          colspan: 1
        }
      }else{
        return {
          rowspan: 0,
          colspan: 0
        }
      }
    }


    return {
      rowspan: 1,
      colspan: 1
    }


    }
  }
};
</script>
